import {getCategoryApi} from "@/apis/categoryApi.js";
import {onMounted, ref} from "vue";
import {onBeforeRouteUpdate, useRoute} from "vue-router";
// 获取分类数据
export function useCategory() {

    const categoryData = ref({})
    const route = useRoute()
    const getCategoryData = async (id = route.params.id) => {
        const res = await getCategoryApi(id)
        categoryData.value = res.result
    }
    onMounted(() => getCategoryData())
    // 监听路由变化
    onBeforeRouteUpdate((to) => getCategoryData(to.params.id))
    return {
        categoryData
    }

}